package leetcodecompetition.S102;

import org.junit.jupiter.api.Test;

import java.util.PriorityQueue;

public class T6336 {
    class Graph {
        int n;
        int[][] distance;

        public Graph(int n, int[][] edges) {
            this.n = n;
            distance = new int[n][n];
            for (int i = 0; i < edges.length; i++) {
                distance[edges[i][0]][edges[i][1]] = edges[i][2];
            }
        }

        public void addEdge(int[] edge) {
            distance[edge[0]][edge[1]] = edge[2];
        }

        public int shortestPath(int node1, int node2) {
            boolean[] boo = new boolean[n];
            PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[0] - b[0]);
            queue.add(new int[]{0, node1});
            while (!queue.isEmpty()) {
                int[] next = queue.poll();
                if (next[1] == node2) {
                    distance[node1][node2]=next[0];
                    return next[0];
                }
                if (boo[next[1]]) {
                    continue;
                } else {
                    boo[next[1]] = true;
                }
                for (int i = 0; i < n; i++) {
                    if (distance[next[1]][i] != 0 && !boo[i]) {
                        queue.add(new int[]{next[0] + distance[next[1]][i], i});
                    }
                }
            }
            return -1;
        }

    }

    @Test
    public void test() {
        Graph graph = new Graph(4, new int[][]{{0, 2, 5}, {0, 1, 2}, {1, 2, 1}, {3, 0, 3}});

        System.out.println(graph.shortestPath(3, 2));


    }
}